﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;
namespace Web
{
    public partial class admin_userupdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int uid = 0;
            if (Request.QueryString["uid"] == null || !Int32.TryParse(Request.QueryString["uid"].ToString(),out uid))
            {
                Response.Redirect("admin_user.aspx");
            }
            if (!IsPostBack) {
                BindData(uid);
            }

        }
        private void BindData(int uid)
        {
            BindUser(uid);
            BindNotice();
        }
        private void BindNotice()
        {
            noticesuccess.Visible = false;
            noticefail.Visible = false;
        }
        private void BindUser(int uid)
        {
            AdminInfo user = AdminInfoLogic.GetOneFromUid(uid);
            lbID.Text = user.AdminID.ToString();
            txbName.Text = user.UserName;
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            int uid = 0;
            if (Request.QueryString["uid"] == null || !Int32.TryParse(Request.QueryString["uid"].ToString(), out uid))
            {
                Response.Redirect("admin_user.aspx");
            }

            AdminInfo olduser = new AdminInfo();
            olduser.UserName = txbName.Text.Trim();
            olduser.Password = txbOldPassword.Text.Trim();
            int isSussess = AdminInfoLogic.GetOneAdminInfo(olduser);
            if (isSussess != 1)
            {
                failtips.InnerText = "上次密码输入错误，请返回重新输入！";
                ChangeStatus(false);
                return;
            }
            AdminInfo user = createModel();
            user.AdminID = uid;
            if (user.Password != user.Password2)
            {
                failtips.InnerText = "两次输入的密码不同，请重新输入！";
                ChangeStatus(false);
                return;
            }
            if (user.Password.Length < 6)
            {
                failtips.InnerText = "为安全考虑，请密码长度大于6位！";
                ChangeStatus(false);
                return;
            }
            if (AdminInfoLogic.UpdateOneAdminInfo(user) == 1)
            {
                ChangeStatus(true);
                return;
            }
            else {
                failtips.InnerText = "数据库异常，如多次发生此错误，请换个时间操作或者联系管理员！";
                ChangeStatus(false);
                return;
            }
        }
        private AdminInfo createModel()
        {
            AdminInfo user = new AdminInfo();
            user.UserName = txbName.Text.Trim();
            user.Password = txbPassword.Text.Trim();
            user.Password2 = txbPassword2.Text.Trim();
            return user;
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();divupload();", true);
        }
    }
}
